package cn.chrilwe.pigpig.im.service.auth;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import cn.chrilwe.pigpig.im.model.TokenStore;
import cn.chrilwe.pigpig.im.service.auth.mark.Oauth2Mark;
import lombok.extern.slf4j.Slf4j;

@Service
@ConditionalOnBean(Oauth2Mark.class)
@Slf4j
public class Oauth2Service {
	
	StringRedisTemplate template;
	
	public Oauth2Service(StringRedisTemplate template) {
		this.template = template;
	}

	public boolean auth(TokenStore store) {
		if(store == null || StringUtils.isEmpty(store.getAccessToken())) {
			return false;
		}
		String token = template.opsForValue().get(store.getAccessToken());
		if(StringUtils.isEmpty(token)) {
			return false;
		}
		return true;
	}
}
